<?php

if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) { die('You are not allowed to call this page directly.'); }

/**
 * creates all tables for the gallery
 * called during register_activation hook
 * 
 * @access internal
 * @return void
 */
function mcms_install () {
	
   	global $wpdb , $wp_roles, $wp_version;
   	
	// Check for capability
	if ( !current_user_can('activate_plugins') ) 
		return;
	
	// Set the capabilities for the administrator
	$role = get_role('administrator');
	// We need this role, no other chance
	if ( empty($role) ) {
		update_option( "mcms_init_check", __('Sorry, NextGEN Gallery works only with a role called administrator',"nggallery") );
		return;
	}

    // upgrade function changed in WordPress 2.3	
	require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    
	// add charset & collate like wp core
	$charset_collate = '';

	if ( version_compare(mysql_get_server_info(), '4.1.0', '>=') ) {
		if ( ! empty($wpdb->charset) )
			$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
		if ( ! empty($wpdb->collate) )
			$charset_collate .= " COLLATE $wpdb->collate";
	}
    
   	$mcms_service = $wpdb->prefix . 'mcms_service';

    // could be case senstive : http://dev.mysql.com/doc/refman/5.1/en/identifier-case-sensitivity.html
	if( !$wpdb->get_var( "SHOW TABLES LIKE '$mcms_service'" ) ) {
      
		$sql = "CREATE TABLE " . $mcms_service . " (
		sid INT(4) NOT NULL AUTO_INCREMENT ,
		name VARCHAR(255) NOT NULL ,
        price FLOAT NOT NULL ,
		description MEDIUMTEXT NULL ,
		update_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
		PRIMARY KEY sid (sid)
		) $charset_collate;";
	
      dbDelta($sql);
    }
    
    $wpdb->insert($mcms_service, array('name'=>'清扫地板', 'price'=>100.00, 'description'=>'Clean the floor'));
    $wpdb->insert($mcms_service, array('name'=>'清扫地板A', 'price'=>60.00, 'description'=>'Clean the floorA'));
    $wpdb->insert($mcms_service, array('name'=>'清扫地板B', 'price'=>50.00, 'description'=>'Clean the floorB'));
    
	// check one table again, to be sure
	if( !$wpdb->get_var( "SHOW TABLES LIKE '$mcms_service'" ) ) {
		update_option( "mcms_init_check", __('MCMS : Tables could not created, please check your database settings',"mcms") );
		return;
	}
	
	$options = get_option('mcms_options');
	// set the default settings, if we didn't upgrade
	if ( empty( $options ) )	
 		mcms_default_options();

}

/**
 * Setup the default option array for the gallery
 * 
 * @access internal
 * @since version 0.33 
 * @return void
 */
function mcms_default_options() {
	
	global $blog_id, $ngg;

	$mcms_options['activateTags']		= false;						// append related images
	
	update_option('mcms_options', $mcms_options);
}

/**
 * Deregister a capability from all classic roles
 * 
 * @access internal
 * @param string $capability name of the capability which should be deregister
 * @return void
 */
function mcms_remove_capability($capability){
//	// this function remove the $capability only from the classic roles
//	$check_order = array("subscriber", "contributor", "author", "editor", "administrator");
//
//	foreach ($check_order as $role) {
//
//		$role = get_role($role);
//		$role->remove_cap($capability) ;
//	}
//
}

/**
 * Uninstall all settings and tables
 * Called via Setup and register_unstall hook
 * 
 * @access internal
 * @return void
 */
function mcms_uninstall() {
	global $wpdb;
	
	// first remove all tables
	$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}mcms_service");
	
	// then remove all options
	delete_option( 'mcms_options' );
}
